package com.yonyou.portcity.utils;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * Created by WangCan on 2018/5/31.
 */
public class AuthUtil {

    public static final String APPID = "wx3094b9477b21e856";
    public static final String APPSECRET = "l-l_7TQsN2bAWXB62Woih-cZBlOS_bcIPyx7aJshMlw";//正式
//    public static final String APPSECRET = "BHkTaT9DB-XVTuwzAhj6cFwUBSHhVLVnbvAExYDIno0";//测试

//    public static final String BACKURL = "http://www.tcggclpower.com:1111";
        public static final String BACKURL = "http://jsoxchem.com:8085/wxtz";//正式
//    public static final String BACKURL = "http://jsoxchem.com:8016/wxtz";//测试
    /**
     * send http request and convert resposne into json object
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static JSONObject doGetJson(String url) throws IOException {
        JSONObject jsonObject = null;
        // DefaultHttpClient client = new DefaultHttpClient();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = client.execute(httpGet);
        //HttpResponse response = client.execute(httpGet);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity, "UTF-8");
                System.out.println(result);
                jsonObject = JSONObject.fromObject(result);
            }
        } finally {
            response.close();
        }
        return jsonObject;
    }

    public static JSONObject doPost(String url, Map<String, String> map) throws IOException {
        CloseableHttpClient httpClient;
        JSONObject jsonObject;
        HttpPost httpPost;
        String result;
        CloseableHttpResponse response =null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            jsonObject = JSONObject.fromObject(map);
            httpPost.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));

             response = httpClient.execute(httpPost);

            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "UTF-8");
                    jsonObject = JSONObject.fromObject(result);

                }
            }
        } finally {
            response.close();
        }
        return jsonObject;
    }
    public JSONObject dourlPost(String url, JSONObject jsonObject) throws IOException {
        CloseableHttpClient httpClient;
        HttpPost httpPost;
        String result;
        CloseableHttpResponse response =null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            //jsonObject = JSONObject.fromObject(map);
            httpPost.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));

             response = httpClient.execute(httpPost);

            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "UTF-8");
                    jsonObject = JSONObject.fromObject(result);

                }
            }
        } finally {
            response.close();
        }
        return jsonObject;
    }

    /**
     * 获取临时素材
     */
    public static InputStream getMediaStream(String mediaId,String access_token)throws IOException {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
//        String url = "https://api.weixin.qq.com/cgi-bin/media/get/jssdk";
        String params = "access_token=" + access_token + "&media_id=" + mediaId;
        InputStream is = null;
        try {
            String urlNameString = url + "?" + params;
            System.out.println(urlNameString);
            URL urlGet = new URL(urlNameString);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            http.connect();
            // 获取文件转化为byte流
            is = http.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return is;
    }


//    public static void main(String[] args) throws IOException {
//        AuthUtil.doGetJson("https://new.udh.yonyouup.com/rs/Deliverys/getDeliveryByDeliveryNo?appkey=dElWBo2z&token=!*G1jweNbfV90mrqsgDI5b8v5zXggYfWl5NJrLtD0Mnn6~RbLmPqxPpjx8fpPs0OSHHLt~eBCqUR7fDjZo6a0E7A**-&format=json&sign=22CE83A444295DA88749C5041EC0B4FA&format=json&deliveryNo=UD-1ae855f010751903270003&appkey=dElWBo2z&token=!*G1jweNbfV90mrqsgDI5b8v5zXggYfWl5NJrLtD0Mnn6~RbLmPqxPpjx8fpPs0OSHHLt~eBCqUR7fDjZo6a0E7A**-");
//
////        AuthUtil authUtil = new AuthUtil();
//
////        String json = "{\n" +
////                "\t\"fileBytes\": \" /9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0A \",\n" +
////                "\t\"fileName\": \" test.jpg \",\n" +
////                "\t\"fileType\": \"jpg \"\n" +
////                "}";
////        JSONObject jsonObject = JSONObject.fromObject(json);
////
////        String url = "http://218.4.137.236:9999/invoice/upload";
////
////        try {
////            JSONObject jsonObject1 =authUtil.dourlPost(url,jsonObject);
////            System.out.println(jsonObject1);
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//    }
}
